﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BOL;
using System.Data.Linq;
using System.Data.SqlClient;
using Website.Logger;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.DataVisualization.Charting;
using ExtAspNet;
using DAL;
using BOL;
using UtilityExtensions;

namespace UILGestion.AlmacenAdmin
{
    public partial class AlmacenAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    idObra.Value = (string)Request.QueryString["idObra"];
                    idAlmacen.Value = (string)Request.QueryString["idAlmacen"];

                    menuCtrl1.loadMenuAlmacen(int.Parse(idObra.Value),int.Parse(idAlmacen.Value)); 
                                              
                    FormatForm();
                }


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }


        }

        private void FormatForm()
        {

            //ListValueBL bol = new ListValueBL();

            int idempresa = DbUtil.Db.Almacens.Single(p => p.idInternal == idAlmacen.Value.ToInt()).idEmpresa.Value;


            ListValueInfo todos = new BOL.ListValueInfo();
            todos.descripcion="-------TODOS-------";
            todos.idInternal=0;


            
            almacenDDL.DataValueField = "idInternal";
            almacenDDL.DataTextField = "descripcion";
            
            //retiramos este almacen
            var lista = new ListValueBL().GetAlmacenesXEmpresa(idempresa);
            var item = lista.Single(p => p.idInternal == idAlmacen.Value.ToInt());

            lista.Remove(item);
            lista.Insert(0, todos);
            almacenDDL.DataSource = lista;
            almacenDDL.DataBind();

         


        }

    }
}